﻿using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.BasicInformation.warehouses;
using THE_WIND_RISES.Common.Domans.Produce.ManifestInfos;
using THE_WIND_RISES.Common.Domans.Warehousing;
using THE_WIND_RISES.Common.Dtos;

namespace Enterprise_Set.API.Application.Commands.Week3.WHAllot_CRUD
{
    /// <summary>
    /// 获取入库单信息
    /// </summary>
    public class GetStorageHander : IRequestHandler<GetStorageCommand, Result>
    {
        //仓库
        private readonly IBaseRepository<Warehouse> WarehouseRepository;
        //入库
        private readonly IBaseRepository<Storage> StorageRepository;
        //物料清单
        private readonly IBaseRepository<ItemManifestInfo> ItemManifestInfoRepository;
        //物料
        private readonly IBaseRepository<Choose> ChooseRepository;
        private readonly IBaseRepository<dynamic> dynamicRepository;

        public GetStorageHander(IBaseRepository<Warehouse> warehouseRepository, IBaseRepository<Storage> storageRepository, IBaseRepository<ItemManifestInfo> itemManifestInfoRepository, IBaseRepository<Choose> chooseRepository, IBaseRepository<dynamic> dynamicRepository)
        {
            WarehouseRepository = warehouseRepository;
            StorageRepository = storageRepository;
            ItemManifestInfoRepository = itemManifestInfoRepository;
            ChooseRepository = chooseRepository;
            this.dynamicRepository = dynamicRepository;
        }

        public async Task<Result> Handle(GetStorageCommand request, CancellationToken cancellationToken)
        {
            Result result = new Result() { Code=(int)CommonErrorCodes.Search};
            var sql = $"select * from Storage as a join ItemManifestInfo as b on a.InventoryInfoId=b.ChooseId join ItemInfo as c on b.ItemManifestCode=c.ItemCode where a.WarehouseId={request.WarehouseSourceId};";
            var StorageData = dynamicRepository.GetAll(sql);
            if (StorageData != null)
            {
                result.Data = StorageData;
                result.Code = (int)CommonErrorCodes.Success;
                return result;
            }
            else
            { 
                return result;
            }
        }
    }
}
